package com.spr.gw.controller.admin;

import com.spr.gw.common.ResultBean;
import com.spr.gw.entity.Category;
import com.spr.gw.entity.SystemConfig;
import com.spr.gw.service.SinglePageService;
import com.spr.gw.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminPageController {

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private SinglePageService singlePageService;

    @RequestMapping("page/{tabName}")
    public String childTab(@PathVariable("tabName") String tabName){
        return "admin/page/" + tabName;
    }

    @RequestMapping("page/{tabName}/{tabItemName}")
    public String childTab2(@PathVariable("tabName") String tabName,
                             @PathVariable("tabItemName") String tabItemName){
        if(StringUtils.isEmpty(tabItemName)){
            return "admin/page/" + tabName;
        }
        return "admin/page/" + tabName + "/" + tabItemName;
    }

    @RequestMapping("/{path}.html")
    public ModelAndView index(@PathVariable("path")String path, ModelAndView mv){
        ResultBean<SystemConfig> systemConfig = systemConfigService.findSystemConfig();
        mv.addObject("config", systemConfig.getData());
        mv.setViewName("admin/" + path);
        return mv;
    }

    @RequestMapping("editSinglePage/{categoryId}")
    public  ModelAndView findOneSinglePage(@PathVariable Integer categoryId, ModelAndView mv){
        Category category = singlePageService.findOne(categoryId).getData();
        mv.addObject("category", category);
        mv.setViewName("admin/page/singlePage/singlePageEdit");
        return mv;
    }
}
